﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PhotoViewer
{
    public partial class frmPicRename : Form
    {
        public frmPicRename()
        {
            InitializeComponent();
        }

        #region 声明字段
        public string imageName;
        public string imagePath;
        public string imageType;
        #endregion

        private void frmPicRename_Load(object sender, EventArgs e)
        {
            this.txtOld.Text= imageName;
            this.txtNew.Text = imageName;
            this.txtNew.SelectAll();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtNew.Text == "")
            {
                MessageBox.Show("The Image Name can't be null", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                string newPath = imagePath.Remove(imagePath.LastIndexOf("\\")) + "\\";//获取图片所在目录

                string newImagePath = newPath + this.txtNew.Text + "." + imageType;//获取图片完整路径

                FileInfo fileInfo = new FileInfo(newImagePath);

                //判断该文件夹下是否已经存在一个同名的文件
                if (fileInfo.Exists)
                {
                    MessageBox.Show("The file name already exists", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                { 
                    //将指定文件移动到新文件
                    File.Move(imagePath, newImagePath);

                    //删除掉原文件
                    File.Delete(imagePath);

                    MessageBox.Show("Renamed Successfully!","Prompting",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    this.Close();
                }

            }
        }

    }
}
